package org.dromara.visitor.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.visitor.domain.dto.*;
import org.dromara.visitor.service.ApplyService;
import org.dromara.visitor.service.MinioService;
import org.dromara.visitor.service.VisConfigService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 访客申请
 */
@RestController
@RequestMapping("/vis/apply")
public class ApplyController {

    @Resource
    private ApplyService applyService;

    @Resource
    private VisConfigService visConfigService;

    @Resource
    private MinioService minioService;
    /**
     * 访客申请提交
     * @param addReq
     * @return
     */
    @PostMapping("/add")
    public R addApply(@RequestBody ApplyAddReq addReq){
      applyService.addApply(addReq);
      return R.ok();
    }

    /**
     * 审核时间设置
     * @param expireTime
     * @return
     */
    @GetMapping("/setExpireTime")
    public R setExpireTime(@RequestParam("expireTime") Integer expireTime){
        visConfigService.setExpireTime("expire_time",expireTime);
        return R.ok();
    }

    /**
     * 查询审核时间
     * @return
     */
    @GetMapping("/getExpireTime")
    public R<Integer> getExpireTime(){
       Integer expireTime = visConfigService.getExpireTime("expire_time");
       return R.ok(expireTime);
    }

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public R<Map<String,String>> uploadFile(@RequestParam("file") MultipartFile file)throws Exception {
        String objectName = UUID.randomUUID().toString();
        String url = minioService.uploadFile(objectName, file.getInputStream(), file.getContentType());
        Map<String,String> map = new HashMap<>();
        map.put("url",url);
        return R.ok(map);
    }

    /**
     * 分页查询
     * @param req
     * @return
     */
    @PostMapping("/queryPageList")
    public R<Page<ApplyResp>> queryPageList(@RequestBody ApplyQueryReq req){
        Page<ApplyResp> page = applyService.queryPageList(req);
        return R.ok(page);
    }

    /**
     * 查询详情
     * @param id
     * @return
     */
    @GetMapping("/queryDetail")
    public R<ApplyDetailResp> queryDetail(@RequestParam("id") Long id){
        ApplyDetailResp applyDetailResp = applyService.queryDetail(id);
        return R.ok(applyDetailResp);
    }

    /**
     * pc端审批
     * @param id
     * @param approveResult  审批结果，1-同意，2-拒绝
     */
    @GetMapping("/pc/approve")
    public R approvePC(@RequestParam("id") Long id,@RequestParam("approveResult") Integer approveResult){
        applyService.approvePC(id,approveResult);
        return R.ok();
    }

    /**
     * 校验姓名手机号是否存在
     * @param visitPhone
     * @param visitName
     * @return
     */
    @GetMapping("/app/validatePhone")
    public R validatePhoneName(@RequestParam("visitPhone") String visitPhone,@RequestParam("visitName") String visitName){
        applyService.validatePhoneName(visitPhone,visitName);
        return R.ok();
    }

    /**
     * app个人列表
     * @param phoneNumber
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/app/list")
    public R<Page<ApplyResp>> queryAppApplyList(@RequestParam("phoneNumber") String phoneNumber,@RequestParam("pageNum") Long pageNum,@RequestParam("pageSize") Long pageSize){
        Page<ApplyResp> page = applyService.queryAppApplyList(phoneNumber,pageNum,pageSize);
        return R.ok(page);
    }

    /**
     * app详情
     * @param id
     * @return
     */
    @GetMapping("/app/detail")
    public R<ApplyDetailResp> appDetail(@RequestParam("id") Long id){
        ApplyDetailResp applyDetailResp = applyService.queryDetail(id);
        return R.ok(applyDetailResp);
    }

    /**
     * app端审核
     * @param id
     * @param approveResult  审批结果，1-同意，2-拒绝
     * @return
     */
    @GetMapping("/app/approve")
    public R approveApp(@RequestParam("id") Long id,@RequestParam("approveResult") Integer approveResult){
        applyService.approveApp(id,approveResult);
        return R.ok();
    }

    /**
     * 导出
     * @param req
     * @param response
     */
    @PostMapping("/export")
    public void export(@RequestBody ApplyQueryReq req, HttpServletResponse response) {
        List<ApplyExportExcel> list = applyService.listExport(req);
        ExcelUtil.exportExcel(list,"访客申请.xlsx" ,"访客申请", ApplyExportExcel.class, response);
    }

}
